<?php

session_start();

require_once '../common/config.php';
require_once '../common/conn.php';
require_once '../common/function.php';
require_once '../db/itemType.db.php';

$action = isset($_POST['action']) ? $_POST['action'] : "";
if ($action == "") {
    $action = isset($_GET['action']) ? $_GET['action'] : "";
}

switch ($action) {
    case 'edit':
        edit();
        break;
    case 'delete':
        delete();
        break;
    case 'create':
        create();
        break;
    default:
        $res['code'] = 2;
        $res['msg'] = '申请类型错误';
}

/**
 * 编辑
 */
function edit() {
    $id = $_POST['idx'];

    $itemTypeName = $_POST['itemTypeName'];

    $dbc = new DBC();
    $itemTypeDb = new ItemTypeDb($dbc);
    $result = $itemTypeDb->modifyItemType($itemTypeName, $id);
    $dbc->close();
    if ($result) {
        echo "<script type='text/javascript'>location.href='../item_type.php'</script>";
    } else {
        echo "<script type='text/javascript'>location.href='../item_type_edit.php?id=" . $id . "'</script>";
    }
}

/**
 * 删除
 */
function delete() {
    $idx = isset($_POST['idx']) ? $_POST['idx'] : isset($_GET['idx']) ? $_GET['idx'] : "";

    $dbc = new DBC();
    $itemTypeDb = new ItemTypeDb($dbc);
    $result = $itemTypeDb->deleteItemType($idx);
    $dbc->close();
    if ($result) {
        echo "<script type='text/javascript'>location.href='../item_type.php'</script>";
    } else {
        echo "<script type='text/javascript'>location.href='../item_type_edit.php?id=" . $idx . "'</script>";
    }
}

/**
 * 新增
 */
function create() {
    $itemTypeName = $_POST['itemTypeName'];

    $dbc = new DBC();
    $itemTypeDb = new ItemTypeDb($dbc);
    $result = $itemTypeDb->createItemType($itemTypeName);
    $dbc->close();
    if ($result) {
        echo "<script type='text/javascript'>location.href='../item_type.php'</script>";
    } else {
        echo "<script type='text/javascript'>location.href='../item_type_edit.php'</script>";
    }
}
